package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.model.*;
import com.qy.service.*;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/charge")
public class ChargeController {
    @Resource
    private ChargeService chargeService;

    @Resource
    private TollService tollService;

    @Resource
    private PolicyOrderService policyOrderService;

    @Resource
    private EnshrineService enshrineService;

    @Resource
    private MemberService memberService;

    @PostMapping("/add")
    public Result add(@RequestBody Charge charge) {
        chargeService.save(charge);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        chargeService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Charge charge) {
        chargeService.update(charge);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Charge charge = chargeService.findById(id);
        return ResultGenerator.successResult(charge);
    }

    @GetMapping("/list")
    public Result list(PageBean<Charge> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Charge> list = chargeService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //优质负责人
    @GetMapping("/highCharge")
    public Result highCharge(PageBean<Charge> page) {
//        PageHelper.startPage(page.getPageNum(), 3, "brower_number desc");
        PageHelper.startPage(page.getPageNum(), 3, "add_time desc");
        List<Charge> list = chargeService.selectAllCharge();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //负责人列表
    @GetMapping("/chargeList")
    public Result chargeList(PageBean<Charge> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
        List<Charge> list = chargeService.selectAllCharge();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //负责人详情
    @GetMapping("/chargeDetail")
    public Result chargeDetail(Charge charge) {
        if (charge.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Charge> list = chargeService.selectChargeById(charge);
        Integer visibleState = 1;//1不可见 2可见
        Integer enshrineState = 1;//1未收藏 2已收藏
        Integer registerState = 1;//1未注册 2已注册
        Map<String, Object> map = new HashMap<>();
        map.put("visibleState", visibleState);
        map.put("enshrineState", enshrineState);
        map.put("registerState", registerState);
        map.put("charge_surplus_number", 0);
        map.put("member_surplus_number", 0);

        Toll toll = tollService.selectOnePolicyPrice();
        map.put("onePolicyPrice", toll.getAmount());

        if (charge.getUser_id() != null) {
            //判断是否可见
            visibleState = policyOrderService.selectVisibleState(charge.getUser_id(), charge.getId(), 3);
            //判断是否收藏
            enshrineState = enshrineService.selectEnshrineState(charge.getUser_id(), charge.getId(), 3);

            Member member = memberService.findById(charge.getUser_id());

            //判断是否注册
            if (member.getPhone() == null || member.getPhone().isEmpty()) {
                registerState = 1;
            } else {
                registerState = 2;
            }

            map.put("visibleState", visibleState);
            map.put("enshrineState", enshrineState);
            map.put("registerState", registerState);
            map.put("charge_surplus_number", member.getCharge_surplus_number());
            map.put("member_surplus_number", member.getMember_surplus_number());
        }
        return ResultGenerator.successResult(list, map);
    }

    //获取手机号
    @GetMapping("/getPhone")
    public Result getPhone(Charge charge, String name, String phone) {
        if (charge.getId() == null || charge.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Condition condition = new Condition(PolicyOrder.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andCondition("user_id =" + charge.getUser_id());
        criteria.andCondition("charge_id =" + charge.getId());
        List<PolicyOrder> list = policyOrderService.findByCondition(condition);
        PolicyOrder policyOrder = list.get(0);
        policyOrder.setName(name);
        policyOrder.setPhone(phone);
        policyOrder.setGet_phone(1);
        policyOrderService.update(policyOrder);
        return ResultGenerator.successResult();
    }

}
